<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/21
 * Time: 17:29
 */

namespace app\api\controller\v2;


use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\model\Comment;
use app\api\model\CommentImage;
use app\api\model\Order;
use app\api\service\OrderService;
use app\api\service\TokenService;
use think\Db;

class CommentController extends BaseController
{
    protected $beforeActionList = [
        'checkLogin' => ['only' => 'createcommentimage,createcomment'],
    ];

    public function getCommentList()
    {
        $result = Db::table('comment')->where('state','1')->field(['id','content','uid','is_hide','reply','create_time',])->select();
        foreach ($result as $key => $value){
            $result[$key]['user'] = Db::table('user')->where('id',$value['uid'])->field(['nickname','avatar_url'])->find();
            $result[$key]['image'] = CommentImage::where('comment_id',$value['id'])->field(['img','img_origin'])->select();
//            $result[$key]['image'] = Db::table('comment_image')->where('comment_id',$value['id'])->field(['img'])->select();
            if($result[$key]['user'] != null){
                $result[$key] = skinArray($result[$key],'user');
                if($value['is_hide'] == 1){
                    $result[$key]['nickname'] = '匿名用户';
                }
            }else{
                unset($result[$key]['user']);
                $result[$key]['nickname'] = '失效用户';
            }
            if(empty($result[$key]['avatar_url'])){
                $result[$key]['avatar_url'] = config('setting.img_prefix').'/uploads/moren.png';
            }
            $result[$key] = hiddenArray($result[$key],['id','uid','is_hide']);
        }
        throw new SuccessMessage([
           'data' => $result,
        ]);
    }

    public function createCommentImage()
    {
        $getFileInfo = uploadFile('file','100','80','uploads/comment');
        $CommentImage = new CommentImage;
        $CommentImage->img =  $getFileInfo['origin'];
        $CommentImage->img_origin = $getFileInfo['tag'];
        $CommentImage->save();
        $id = $CommentImage->id;
        if(!$id){
            throw new MissException();
        }
        return $id;
    }

    public function createComment()
    {
        (new \app\api\validate\Comment())->goCheck();
        $order_id = input('order_id/id');
        OrderService::checkOrderInUser($order_id);
//        $res = Comment::where('order_id',$order_id)->find();
        $order = Order::get($order_id);
        if($order['is_comment'] == '已评价'){
            throw new MissException([
                'msg' => '您已经评价过了',
            ]);
        }
        if($order['status'] !== '订单完成'){
            throw new MissException([
                'msg' => '订单未完成',
            ]);
        }

        Db::startTrans();
        try{
            $comment = new Comment;
            $comment->content = input('content');
            $comment->is_hide = input('is_hide');
            $comment->order_id = $order_id;
            $comment->uid = TokenService::getCurrentUid();
            $comment->save();
            $comment_id = $comment->id;
            //修改订单状态
            Order::where('id',$order_id)->update(['is_comment' => '2']);
            $image_id = input('post.');

            if(isset($image_id['image_id'])){
                $CommentImage = new CommentImage;
                $CommentImage->where('id','in',$image_id['image_id'])->update(['comment_id' => $comment_id]);
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new MissException();
        }
        return new SuccessMessage();
    }
}